'use strict';

let products = [
    {
        id: 1,
        productName: '薯片',
        price: 20,
        stock: 100
    },
    {
        id: 2,
        productName: '牛奶',
        price: 25,
        stock: 100
    }
]

let fn_list = async (ctx, next) => {
    let keyword = ctx.request.query.keyword || '';
    if (keyword) {
        let arr = products.filter(item => {
             return item.id == keyword || item.productName == keyword || item.price == keyword || item.stock == keyword
        })
        ctx.body = {
            code:'1000',
            data:arr,
            msg:"请求成功!"
        }
    }else{
        ctx.body = {
            code: 1000,
            data: products,
            msg: '请求成功'
        }
    }
}

let fn_post = async (ctx, next) => {

}

let fn_put = async (ctx, next) => {

}

let fn_delete = async (ctx, next) => {

}

let fn_getById = async (ctx, next) => {

}

module.exports = {
    'get /product': fn_list,
    'post /product': fn_post,
    'put /product:id': fn_put,
    'delete /product:id': fn_delete,
    'get /product:id': fn_getById
}